export default class PlayerSprite extends godot.Sprite {
    x = 200
    y = 200
    v = 3

    isInput = false;
    isKeyMove = false;
    isMouseMove = false;

    // 人物朝向 --> 控制哪一排的
    face = 0;
    // 动画帧 --> 每一排中第几个
    frameIndex = 0

    frameStart = 0
    frameEnd = 4

    constructor() {
        super();

        this.texture = godot.load("res://assets/player.png") as godot.Texture;
        this.hframes = 5
        this.vframes = 4

        this.position = new godot.Vector2(this.x, this.y)

        this.isKeyMove = true;
        this.isMouseMove = true
    }

    // 逻辑帧 60hz的倍数 
    _process(delta: number): void {

    }

    // 物理帧 严格的60hz(1s执行60次) 事件处理 位置相关
    _physics_process(delta: number): void {
        if (this.isKeyMove) {
            // this.handleKey()

            this.handlekAction()
        }

        if (this.isMouseMove) {
            if (godot.Input.is_mouse_button_pressed(godot.BUTTON_LEFT)) {
                let mousePos = this.get_viewport().get_mouse_position()
                this.x = mousePos.x
                this.y = mousePos.y
                this.position = mousePos
            }
        }

    }

    handleKey() {
        let face = -1
        // InputAction
        if (godot.Input.is_key_pressed(godot.KEY_W)) {
            console.log("W")
            face = 3
            this.y = this.y - this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_A)) {
            console.log("A")

            face = 1
            this.x = this.x - this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_S)) {
            console.log("S")

            face = 0
            this.y = this.y + this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_D)) {
            console.log("D")
            face = 2
            this.x = this.x + this.v
        }
        this.position = new godot.Vector2(this.x, this.y)

        if (face > -1) {
            //有按键移动
            if (face == this.face) {
                //当前动画播放
                if (this.frameIndex < this.frameEnd) {
                    this.frameIndex = this.frameIndex + 0.2
                } else {
                    this.frameIndex = 0
                }
            } else {
                // 切换到新动画
                this.face = face
                this.frameIndex = 0
            }
            this.frame_coords = new godot.Vector2(this.frameIndex, this.face)
        } else {
            //没有按键移动
        }
    }

    handlekAction() {
        let face = -1
        // InputAction
        if (godot.Input.is_action_pressed("ui_up")) {
            console.log("ui_up")
            face = 3
            this.y = this.y - this.v
        } else if (godot.Input.is_action_pressed("ui_left")) {
            console.log("ui_left")
            face = 1
            this.x = this.x - this.v
        } else if (godot.Input.is_action_pressed("ui_down")) {
            console.log("ui_down")
            face = 0
            this.y = this.y + this.v
        } else if (godot.Input.is_action_pressed("ui_right")) {
            console.log("ui_right")
            face = 2
            this.x = this.x + this.v
        }
        this.position = new godot.Vector2(this.x, this.y)

        if (face > -1) {
            //有按键移动
            if (face == this.face) {
                //当前动画播放
                if (this.frameIndex < this.frameEnd) {
                    this.frameIndex = this.frameIndex + 0.2
                } else {
                    this.frameIndex = 0
                }
            } else {
                // 切换到新动画
                this.face = face
                this.frameIndex = 0
            }
            this.frame_coords = new godot.Vector2(this.frameIndex, this.face)
        } else {
            //没有按键移动
        }
    }
    // 不涉及位置相关  打开背包 与敌人对话
    _input(event: godot.InputEvent): void {
        if (this.isInput == false) {
            return
        }
        if (this.isKeyMove && event instanceof godot.InputEventKey && event.is_pressed()) {
            let face = -1
            if (event.scancode == godot.KEY_W) {
                face = 3
                this.y = this.y - this.v
            } else if (event.scancode == godot.KEY_A) {
                face = 1
                this.x = this.x - this.v
            } else if (event.scancode == godot.KEY_S) {
                face = 0
                this.y = this.y + this.v
            } else if (event.scancode == godot.KEY_D) {
                face = 2
                this.x = this.x + this.v
            }

            if (face > -1) {
                //有按键移动
                if (face == this.face) {
                    //当前动画播放
                    if (this.frameIndex < this.frameEnd) {
                        this.frameIndex = this.frameIndex + 0.2
                    } else {
                        this.frameIndex = 0
                    }
                } else {
                    // 切换到新动画
                    this.face = face
                    this.frameIndex = 0
                }
                this.frame_coords = new godot.Vector2(this.frameIndex, this.face)
            } else {
                //没有按键移动
            }

            this.position = new godot.Vector2(this.x, this.y)
        }

        if (this.isMouseMove && event instanceof godot.InputEventMouseButton && event.is_pressed()) {
            if (event.button_index == godot.BUTTON_LEFT) {
                this.x = event.position.x
                this.y = event.position.y
                this.position = event.position
            }
        }
    }





}